package com.ioPackage;


import java.io.*;

/**
 * @ClassName FileIODemo
 * @Description 字节输入流、输出流
 * @Author 2214361854
 * @Date 2022/2/27 19:53
 * @Version 1.0
 */
public class FileIODemo {
    public static void main(String[] args) throws Exception {
        System.out.println("====输出流，写文件====");
        //加true，追加内容，不清空
        OutputStream fi = new FileOutputStream("src\\text01.txt");
        //包装成缓冲流，带8kb缓冲
        OutputStream bfi = new BufferedOutputStream(fi);
        bfi.write("你好，中国".getBytes());

        fi.flush();
        bfi.write("\r\nabc123".getBytes());

        fi.close();
        System.out.println("写入完成");

        System.out.println("====输入流，读文件====");
//        InputStream is = new FileInputStream("src\\text01.txt");

//        int read;
//        while ((read = is.read())!=-1) {
//            System.out.print((char) read);
//        }

//        byte[] bytes = new byte[3];
//        int len = is.read(bytes);
//        String str = new String(bytes,0,len);
//        System.out.println(str);

//        byte[] bytes2 = new byte[1024];
//        int len ;
//        while ((len = is.read(bytes2)) != -1 ) {
//            System.out.print(new String(bytes2,0,len));
//        }
//        System.out.println();

        /**
         * jdk9 readAllBytes()原理  读取全部字节数组
         */
        File f = new File("src\\text01.txt");
        InputStream is2 = new FileInputStream("src\\text01.txt");
        //包装成缓冲流，带8kb缓冲
        InputStream bis = new BufferedInputStream(is2);
//        (int)f.length() 一次读取全部字节
        byte[] bu2 = new byte[1024];
        bis.read(bu2);
        System.out.print(new String(bu2));


    }
}
